Skip to main content

Lamport Timestamps

Lamport Timestamps is a pattern used in Distributed Systems to achieve sort of an sequential ordering for a certain set of events. By using Timestamps are source of truth we can, at least ensure that the order which operations were applied are ordered. It falls within the so-called Sequencer Number Ordering - since this is a monotonically increasing value. The difference between some common sequence number generator AKA some incremental number from Lamport Timestamps is the fact that we can use Lamport in distributed models like Leaderless and Multi-Leader topologies. Lamport Timestamp can achieve causality differently from other algorithms.

Algorithm

For a set of servers, each one should have it's own node-id and count each operation in a monotonically increasing value. Yes, two servers can have the same counter but will differ by node-id Whenever a node receives a request, the counter value is always passed through. If receiving peer value is lower, it gets updated with this new value. This way, whenever services have causal dependencies - at communication time they update their peer's with the latest value ensuring an causality between counter values.

Careful

Lamport Timestamp implementations/use-case can be easily mistaken by Version Vectors - which have different purposes. Version Vectors are meant to decide wether two operations are concurrent or if it has any sort of causality dependency. Lamport Timestamps enforce the ordering of events.

Notes

It might not seem so interesting at first but on asynchronous environments - like Messaging Systems - having some sort of causality to depend on can really help out. Some queues may lag behind and cause weird behavior - Lamport Timestamps can help with that.